package com.sky.myapplication.kotlin.kotlinAdapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.sky.myapplication.R
import com.sky.myapplication.kotlin.data.ListBean
import com.sky.myapplication.kotlin.kotlinInterface.ItemClickKotlinInterface

/**
 * @Author: Minxp
 * @UpdateDate: 2021/7/5 0005
 * @Description: recycleView的Adapter
 */
class RecycleAdapter(val context: Context, val data : List<ListBean>) : RecyclerView.Adapter<RecycleAdapter.RecycleViewHolder>() {
    var itemClickListener: ItemClickKotlinInterface? = null

    fun setOnItemClickListener(itemClickListener: ItemClickKotlinInterface){
        this.itemClickListener = itemClickListener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecycleViewHolder {
        return RecycleViewHolder(LayoutInflater.from(context).inflate(R.layout.item_recycle_list, parent, false))
    }

    override fun onBindViewHolder(holder: RecycleViewHolder, position: Int) {
        holder.tv_name.text = data[position].name
        if (data.get(position).isSelect){
            holder.iv_check.setImageDrawable(context.resources.getDrawable(R.drawable.rv_selected))
        } else {
            holder.iv_check.setImageDrawable(context.resources.getDrawable(R.drawable.rv_normal))
        }
        holder.ll_item.setOnClickListener{
            itemClickListener?.onItemClick(position)
        }
    }

    override fun getItemCount(): Int {
        return data.size
    }

    class RecycleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var tv_name: TextView = itemView.findViewById(R.id.tv_name)
        var iv_check: ImageView = itemView.findViewById(R.id.iv_check)
        var ll_item: ConstraintLayout = itemView.findViewById(R.id.ll_item)
    }
}